package com.pearadmin.pro.modules.not.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;
import com.pearadmin.pro.modules.not.domain.SysInbox;
import com.pearadmin.pro.modules.not.domain.SysInboxreply;
import com.pearadmin.pro.modules.not.param.SysInboxRequest;
import com.pearadmin.pro.modules.not.repository.SysInboxRepository;
import com.pearadmin.pro.modules.not.service.SysInboxService;
import com.pearadmin.pro.modules.sys.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysInboxServiceImpl extends ServiceImpl<SysInboxRepository, SysInbox> implements SysInboxService {

    @Resource
    private SysInboxRepository sysInboxRepository;

    @Resource // security 基本信息处理类
    private UserContext userContext;

    @Override
    public List<SysInbox> list(SysInboxRequest request) {
        return sysInboxRepository.selectInbox(request);
    }

    @Override
    public PageResponse<SysInbox> page(SysInboxRequest request) {
        return Pageable.of(request, (() -> sysInboxRepository.selectInbox(request)));
    }

    /**
     * 查看具体私信内容（多条）
     *
     * @param param
     * @return java.util.List<com.pearadmin.pro.modules.not.domain.SysInboxreply>
     * @author ZhouXingYun
     * @date 2022/5/9 15:01
     */
    @Override
    public List<SysInboxreply> getLetterCount(String param) {
        List<SysInboxreply> list = sysInboxRepository.getLetterCount(param);
        return list;
    }

    /**
     * 消息回复
     *
     * @param sysInboxreply
     * @return boolean
     * @author ZhouXingYun
     * @date 2022/5/9 18:58
     */
    @Override
    public boolean messageReply(SysInboxreply sysInboxreply) {
        // 获取当前登录用户信息
        SysUser principal = userContext.getPrincipal();
        if (principal.getId().equals(sysInboxreply.getName1())) {
            // 如果名称符合当前登录的用户 ， 说明是当前name2就是需要接收的用户
            sysInboxreply.setCreateBy(sysInboxreply.getName1());
            sysInboxreply.setRecipientId(sysInboxreply.getName2());
        } else {
            // 反之则发送人是name2
            sysInboxreply.setCreateBy(sysInboxreply.getName2());
            sysInboxreply.setRecipientId(sysInboxreply.getName1());

        }
        sysInboxreply.setIsRead(String.valueOf(0));
        sysInboxreply.setSendTime(new Date());
        sysInboxreply.setCreateTime(new Date());
        int a = sysInboxRepository.messageReply(sysInboxreply);
        return a>0;
    }

    /**
     * 新增消息记录
     *
     * @param sysInbox
     * @return boolean
     * @author ZhouXingYun
     * @date 2022/5/10 17:43
     */
    @Override
    public boolean insertReply(SysInbox sysInbox) {
        // 添加对话信息
        SysInboxreply sysInboxreply = new SysInboxreply();
        // 获取当前登录用户信息
        SysUser principal = userContext.getPrincipal();
        sysInboxreply.setCreateBy(principal.getId());
        sysInboxreply.setRecipientId(sysInbox.getRecipientId());
        sysInboxreply.setIsRead(String.valueOf(0));
        sysInboxreply.setDialogueId(sysInbox.getDialogueId());
        sysInboxreply.setSendTime(new Date());
        sysInboxreply.setContent(sysInbox.getContent());
        sysInboxreply.setCreateTime(new Date());
        int a = sysInboxRepository.messageReply(sysInboxreply);
        return a>0;
    }
}
